package vin.pth.example.authrization.token;

import java.util.ArrayList;
import java.util.Collection;
import lombok.Getter;
import vin.pth.base.pojo.Authentication;
import vin.pth.base.pojo.GrantedAuthority;
import vin.pth.base.pojo.UserDetails;

/**
 * @author Cocoon
 */
@Getter
public class SmsCodeToken implements Authentication {

  private final String username;
  private final String smsCode;
  private final UserDetails userDetails;
  private final Boolean authenticated;

  public SmsCodeToken(String username, String smsCode) {
    this.username = username;
    this.smsCode = smsCode;
    this.userDetails = null;
    this.authenticated = false;

  }

  public SmsCodeToken(String username, String smsCode, UserDetails userDetails) {
    this.username = username;
    this.smsCode = smsCode;
    this.userDetails = userDetails;
    this.authenticated = true;

  }

  /**
   * 主要内容,用户定位用户.
   *
   * @return String
   */
  @Override
  public String getPrincipal() {
    return username;
  }

  /**
   * 凭证,用户的认证凭证.
   *
   * @return String
   */
  @Override
  public String getCredentials() {
    return smsCode;
  }

  /**
   * 用户信息.
   *
   * @return UserDetails
   */
  @Override
  public UserDetails getUserDetails() {
    return userDetails;
  }

  /**
   * 是认证验通过.
   *
   * @return true=通过
   */
  @Override
  public boolean isAuthenticated() {
    return authenticated;
  }

  /**
   * 用户具备的权限.
   *
   * @return GrantedAuthority
   */
  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    if (userDetails == null) {
      return new ArrayList<>();
    }
    return userDetails.getAuthority();
  }
}
